Page({
  data: {
    questionnaire: null,
    currentQuestion: 0,
    answers: [],
    loading: true,
    progress: 0
  },

  onLoad(options) {
    const { id } = options;
    // 使用模拟数据
    const mockQuestionnaire = {
      id: id,
      title: '睡眠质量评估问卷V2.0',
      version: '2.0',
      questions: [
        {
          id: 'q1',
          type: 'radio',
          title: '您平均每天的睡眠时间是多少？',
          options: [
            { value: 'A', label: '少于6小时' },
            { value: 'B', label: '6-8小时' },
            { value: 'C', label: '8-10小时' },
            { value: 'D', label: '超过10小时' }
          ]
        },
        {
          id: 'q2',
          type: 'scale',
          title: '您对当前的睡眠质量满意吗？',
          options: []
        },
        {
          id: 'q3',
          type: 'checkbox',
          title: '您是否有以下睡眠问题？（可多选）',
          options: [
            { value: 'A', label: '入睡困难' },
            { value: 'B', label: '容易醒来' },
            { value: 'C', label: '多梦' },
            { value: 'D', label: '打鼾' }
          ]
        },
        {
          id: 'q4',
          type: 'text',
          title: '请描述一下您的睡眠习惯：',
          options: []
        }
      ]
    };

    // 初始化答案数组
    const answers = mockQuestionnaire.questions.map(q => ({
      questionId: q.id,
      type: q.type,
      value: null
    }));

    // 模拟加载延迟
    setTimeout(() => {
      this.setData({
        questionnaire: mockQuestionnaire,
        answers,
        loading: false,
        progress: 0
      });
    }, 500);
  },

  // 处理单选题答案
  handleRadioChange(e) {
    const { questionIndex } = e.currentTarget.dataset;
    const value = e.detail.value;
    this.updateAnswer(questionIndex, value);
  },

  // 处理多选题答案
  handleCheckboxChange(e) {
    const { questionIndex } = e.currentTarget.dataset;
    const value = e.detail.value;
    this.updateAnswer(questionIndex, value);
  },

  // 处理量表题答案
  handleScaleChange(e) {
    const { questionIndex } = e.currentTarget.dataset;
    const value = e.detail.value;
    this.updateAnswer(questionIndex, value);
  },

  // 处理文本输入答案
  handleTextInput(e) {
    const { questionIndex } = e.currentTarget.dataset;
    const value = e.detail.value;
    this.updateAnswer(questionIndex, value);
  },

  // 更新答案
  updateAnswer(questionIndex, value) {
    const { answers } = this.data;
    answers[questionIndex].value = value;
    
    // 计算进度
    const progress = (answers.filter(a => a.value !== null).length / answers.length) * 100;
    
    this.setData({
      answers,
      progress
    });
  },

  // 上一题
  prevQuestion() {
    if (this.data.currentQuestion > 0) {
      this.setData({
        currentQuestion: this.data.currentQuestion - 1
      });
    }
  },

  // 下一题
  nextQuestion() {
    if (this.data.currentQuestion < this.data.questionnaire.questions.length - 1) {
      this.setData({
        currentQuestion: this.data.currentQuestion + 1
      });
    } else {
      this.submitQuestionnaire();
    }
  },

  // 提交问卷
  submitQuestionnaire() {
    wx.showLoading({
      title: '提交中...'
    });

    // 模拟提交延迟
    setTimeout(() => {
      wx.hideLoading();
      wx.showToast({
        title: '提交成功',
        icon: 'success',
        duration: 2000,
        success: () => {
          // 延迟返回上一页
          setTimeout(() => {
            wx.navigateBack();
          }, 2000);
        }
      });
    }, 1500);
  }
}); 